Views is commonly hit by a bug in db_rewrite_sql covered in the Core bug #284392: db_rewrite_sql causing issues with DISTINCT.

Rather than have each module using views implementing a workaround, this patch provides a workaround in views to deal with the problem.

I think it needs to look to see the base table is node still but it seems to work.

Comments

neclimdul’s picture

StatusFileSize
new684 bytes

Try 2 with a base_table check.

neclimdul’s picture

Status: Active » Needs review
StatusFileSize
new946 bytes

Fixed bug where false wasn't set when set_distinct was called and an access module was present. Better docs.

neclimdul’s picture

StatusFileSize
new864 bytes

Accidently fudged the parameters of the outer if at some point. replace them.

karens’s picture

Status: Needs review » Reviewed & tested by the community

Looks good to me!! I'll be happy to see this nasty bug get a work-around :)

merlinofchaos’s picture

Version: 6.x-2.x-dev » 6.x-3.x-dev
Status: Reviewed & tested by the community » Needs work

Committed to 2.x. We're going to need a reroll against the 3.x branch.

dawehner’s picture

Status: Needs work » Needs review
StatusFileSize
new1.18 KB

here is a fast rerole

igama’s picture

Version: 6.x-3.x-dev » 6.x-2.x-dev

I have:
Latest Drupal 6
Views 6.x-2.x-dev 2009-Jun-26
Workflow 6.x-1.1

I had the SQL error when I would try and activate the Distinct ( SELECT DISTINCT(node.nid) AS DISTINCT(node.nid)....) , and then I updated to the latest dev View and now I get no error, but the duplicates are still there.

There is no "DISTINCT(node.nid)" in the SQL.

SELECT node.nid AS nid,
node_data_field_image.field_image_fid AS node_data_field_image_field_image_fid,
node_data_field_image.field_image_list AS node_data_field_image_field_image_list,
node_data_field_image.field_image_data AS node_data_field_image_field_image_data,
node.type AS node_type,
node.vid AS node_vid,
term_data.name AS term_data_name,
term_data.vid AS term_data_vid,
term_data.tid AS term_data_tid,
node.title AS node_title,
vocabulary.name AS vocabulary_name,
node_revisions.body AS node_revisions_body,
node_revisions.format AS node_revisions_format,
node.sticky AS node_sticky,
node_data_field_date.field_date_value AS node_data_field_date_field_date_value,
DATE_FORMAT((FROM_UNIXTIME(node.created) + INTERVAL 28800 SECOND), '%Y%m%d%H%i') AS node_created_minute
FROM node node
LEFT JOIN workflow_node workflow_node ON node.nid = workflow_node.nid
LEFT JOIN content_field_image node_data_field_image ON node.vid = node_data_field_image.vid
LEFT JOIN term_node term_node ON node.vid = term_node.vid
LEFT JOIN term_data term_data ON term_node.tid = term_data.tid
LEFT JOIN vocabulary vocabulary ON term_data.vid = vocabulary.vid
LEFT JOIN node_revisions node_revisions ON node.vid = node_revisions.vid
LEFT JOIN content_field_date node_data_field_date ON node.vid = node_data_field_date.vid
WHERE (node.type in ('equipamento')) AND (node.status <> 0 OR (node.uid = ***CURRENT_USER*** AND ***CURRENT_USER*** <> 0) OR ***ADMINISTER_NODES*** = 1) AND (workflow_node.sid = 4)
ORDER BY node_sticky DESC, node_data_field_date_field_date_value DESC, node_created_minute DESC, node_title ASC

merlinofchaos’s picture

Version: 6.x-2.x-dev » 6.x-3.x-dev

From the help text on the distinct setting:

This will make the view display only distinct items. If there are multiple identical items, each will be displayed only once. You can use this to try and remove duplicates from a view, though it does not always work.

Please actually read stuff before you post and change the status of issues around. It doesn't always work. It says so. There's nothing I can do about it. If DISTINCT doesn't do the trick then you need a new approach.

stormsweeper’s picture

#7: With that query, if you have more than one term or file on a node you will get multiple rows for that node, and each will be distinct as far as the db is concerned.

merlinofchaos’s picture

Status: Needs review » Fixed

Committed to 3.x line too. Thanks!

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

agentrickard’s picture

Status: Closed (fixed) » Active

I think 6.14 may have broken this. See #284392: db_rewrite_sql causing issues with DISTINCT, comments after #154.

ken54671’s picture

StatusFileSize
new828 bytes

Patch for Views 6.x-2.6.

ken54671’s picture

A cleaner implementation of #13. Patch for Views 6.x-2.6.

ken54671’s picture

StatusFileSize
new645 bytes

A cleaner implementation of #13. Patch for Views 6.x-2.6.

dummas_324324_32’s picture

It works!!! very nice work..

The weekend can start!

Frank Steiner’s picture

Works fine for the problen with 6.14 and -2.x.

izmeez’s picture

subscribe

merlinofchaos’s picture

I need a patch that works with both 6.13 and 6.14 -- because I can't guarantee that anybody will be running a particular version.

merlinofchaos’s picture

Actually: Please post the patch in http://drupal.org/node/579892 instead and return this to its original state.

agentrickard’s picture

6.14 was a security release, so we can force people to be on that version, yes?

izmeez’s picture

I applied the patch in #15 to the latest 6.x-2.x-dev version and it has solved the problems of duplicates and triplicates in Drupal 6.14

Thanks,

Izzy

ken54671’s picture

Status: Active » Closed (fixed)

Closing this issue since these workarounds are for versions of Drupal before 6.14.

If you are using Drupal 6.14, please see Authors and only authors see dupplicate items after upgrading Drupal to 6.14 and db_rewrite_sql causing issues with DISTINCT.

damien tournoud’s picture

Status: Closed (fixed) » Active

I do not get the patch that went in (#3): why *in hell* do you prevent distinct for being set if the base table is not node?

R.Hendel’s picture

subscribe

Ole Martin’s picture

And where to put this patch ?

merlinofchaos’s picture

DamZ is right. It should prevent distinct from being set if the base table is 'node'.

The original patch here needs to be reverted.

merlinofchaos’s picture

Original patch reverted from 2.x and 3.x versions.

Leeteq’s picture

Subscribing.

damien tournoud’s picture

Status: Active » Closed (duplicate)